package mamma.dao;

import java.util.List;

import mamma.common.MybatisConfig;
import mamma.vo.BabyDailyVo;
import mamma.vo.BabyInfoVo;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class BabyDao {
	private SqlSessionFactory factory;
	
	public BabyDao(){
		factory = MybatisConfig.getSqlSessionFactory();
	}
	
	public List<BabyInfoVo> goBabyInfo(String email){
		SqlSession ss = factory.openSession();
		List<BabyInfoVo> list = ss.selectList("baby.goBabyInfo", email);
		ss.close();
		
		return list;
	}
	
	// insert baby (at first time)
	public void insertBaby(BabyInfoVo babyInfo) {
		SqlSession ss = factory.openSession();
		ss.insert("baby.insertBaby", babyInfo);
		ss.commit();
		
		int babyNum = ss.selectOne("baby.selectBaby", babyInfo);
		ss.commit();
		babyInfo.setBabynum(babyNum);
		
		ss.insert("baby.insertBaby2", babyInfo);
		ss.commit();
		
		ss.update("baby.updateHasBaby", babyInfo.getMemberemail());
		ss.commit();
		
		ss.close();
	}
	
	public BabyInfoVo selectBaby(BabyInfoVo babyInfo) {
		SqlSession ss = factory.openSession();
		BabyInfoVo tempVo = ss.selectOne("baby.selectBaby", babyInfo);
		ss.close();
		
		return tempVo;
	}
	
	public List<BabyInfoVo> selectBabyList(BabyInfoVo vo){
		SqlSession ss = factory.openSession();
		List<BabyInfoVo> list = ss.selectList("baby.selectBabyList", vo);
		ss.close();
		
		return list;
	}
	
	
	
	// ====================== baby info ======================
	
	//입력
	public int insertBabyInfo(BabyInfoVo baby){
		int result = 0;
		SqlSession ss = null;

		try {
			ss = factory.openSession();
			result = ss.insert("baby.insertBabyInfo", baby);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(ss != null) ss.close();
		}

		return result;
	}

	//목록
	public List<BabyInfoVo> listBabyInfo(int babyNum){
		List<BabyInfoVo> list = null;
		SqlSession ss = null;

		try {
			ss = factory.openSession();
			list = ss.selectList("baby.listBabyInfo", babyNum);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(ss != null) ss.close();
		}

		return list;
	}

	//목록(3개만출력_Home화면)
	public List<BabyInfoVo> listBabyInfoThree(int babynum){
		List<BabyInfoVo> list = null;
		SqlSession ss = null;

		try {
			ss = factory.openSession();
			list = ss.selectList("baby.listBabyInfoThree", babynum);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(ss != null) ss.close();
		}

		return list;
	}
	
	// =========================================
	
	//입력
	public int insertBabyData(BabyDailyVo baby){
		int result = 0;
		SqlSession ss = null;

		try {
			ss = factory.openSession();
			result = ss.insert("baby.insertBabyData", baby);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(ss != null) ss.close();
		}

		return result;
	}

	//목록 및 차트구현
	public BabyDailyVo listBabyData(BabyDailyVo vo){
		SqlSession ss = null;
		BabyDailyVo outputVo = null;

		try {
			ss = factory.openSession();
			outputVo = ss.selectOne("baby.listBabyData", vo);
			ss.commit();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			if(ss != null) ss.close();
		}

		return outputVo;
	}
	
}
